package com.ksana.orm.config;

import com.ksana.orm.core.io.impl.PropertiesReader;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Map;

/**
 * 读取信息
 *
 * @author Fall
 */
public class Configuration {
    private Map<String, Object> map = null;
    private Connection connection = null;

    public Connection build() {
        // 加载数据库配置信息
        map = new PropertiesReader().reader("classpath:application.properties");
        return evalDataSource(map);
    }

    private Connection evalDataSource(Map<String, Object> map) {
        if (connection != null) {
            return connection;
        }

        // 读取数据库信息
        try {
            Class.forName((String) map.get("database.drive"));
            connection = DriverManager.getConnection((String) map.get("database.url"), (String) map.get("database.username"), (String) map.get("database.password"));
            return connection;
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return null;
    }
}